package controllers;

import db.TimeSlotDAO;
import java.util.List;
import java.sql.Time;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

@ManagedBean
@SessionScoped
public class TimeSlot {

    int id;
    int driverID;
    int startHr;
    int startMin;
    String startMer;
    int endHr;
    int endMin;
    String endMer;
    String weekday;
    TimeDisp startTime;
    TimeDisp endTime;
    String driverName;
    String message;
    private static TimeSlot editableTimeSlot;
    private static final SelectItem[] weekdays = {
        new SelectItem("Monday", "Monday"),
        new SelectItem("Tuesday", "Tuesday"),
        new SelectItem("Wednesday", "Wednesday"),
        new SelectItem("Thursday", "Thursday"),
        new SelectItem("Friday", "Friday"),
        new SelectItem("Saturday", "Saturday"),
        new SelectItem("Sunday", "Sunday")};

    //<editor-fold desc="Constructors">
    public TimeSlot() {
    }

    //Used in retrieving TimeSlots
    public TimeSlot(int id, int driverID, String weekday, Time startTime, Time endTime, String driverName) {
        this(driverID, weekday, startTime, endTime, driverName);
        this.id = id;
    }

    public TimeSlot(int driverID, String weekday, Time startTime, Time endTime, String driverName) {
        this();
        this.driverID = driverID;
        this.weekday = weekday;
        this.startTime = new TimeDisp(startTime);
        this.endTime = new TimeDisp(endTime);
        this.driverName = driverName;
    }
    //</editor-fold>

    //<editor-fold desc="Getters and Setters">
    public int getId() { //Primary Key, no setter
        return id;
    }

    public void setDriverID(int driverID) { //Foreign Key, no setter
        this.driverID = driverID;
    }

    public int getDriverID() {
        return driverID;
    }

    public String getWeekday() {
        return weekday;
    }

    public void setWeekday(String day) {
        this.weekday = day;
    }

    public void setStartTime() {
        if (startMer.equals("PM") && startHr == 12) {
        } //Do nothing
        else if (startMer.equals("PM")) {
            startHr += 12;
        } //If not 12, then add 12
        else if (startMer.equals("AM") && startHr == 12) {
            startHr -= 12;
        }//If 12 AM, subtract 12 when adding
        startTime = new TimeDisp(startHr, startMin);
    }

    public void setEndTime() {
        if (endMer.equals("PM") && endHr == 12) {
        } //Do nothing
        else if (endMer.equals("PM")) {
            endHr += 12;
        } //If not 12 and PM, then add 12
        else if (endMer.equals("AM") && endHr == 12) {
            endHr -= 12;
        }//If 12 AM, subtract 12 when adding
        endTime = new TimeDisp(endHr, endMin);
    }

    public void setStartTime(Time time) {
        this.startTime = (TimeDisp)time;
    }

    public void setEndTime(Time time) {
        this.endTime = (TimeDisp)time;
    }

    public Time getStartTime() {
        return startTime;
    }

    public Time getEndTime() {
        return endTime;
    }

    public String getDriverName() {
        return driverName;
    }

    public void setStartHr(int startHr) {
        this.startHr = startHr;
    }

    public void setStartMin(int startMin) {
        this.startMin = startMin;
    }

    public void setStartMer(String startMer) {
        this.startMer = startMer;
    }

    public void setEndHr(int endHr) {
        this.endHr = endHr;
    }

    public void setEndMin(int endMin) {
        this.endMin = endMin;
    }

    public void setEndMer(String endMer) {
        this.endMer = endMer;
    }

    public int getStartHr() {
        return startHr;
    }

    public int getStartMin() {
        return startMin;
    }

    public String getStartMer() {
        return startMer;
    }

    public int getEndHr() {
        return endHr;
    }

    public int getEndMin() {
        return endMin;
    }

    public String getEndMer() {
        return endMer;
    }

    public SelectItem[] getWeekdays() {
        return weekdays;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }
    //</editor-fold>

    //<editor-fold desc="Editable TimeSlot">
    public void setEditableTimeSlot(TimeSlot timeSlot) {
        editableTimeSlot = timeSlot;
    }

    //Non-static method for JSF access
    public TimeSlot getEditableTimeSlot() {
        return editableTimeSlot;
    }

    //Static method for Java access
    public static TimeSlot getEditableTimeSlotStatic() {
        return editableTimeSlot;
    }

    public String editTimeSlot() {
        FacesContext fc = FacesContext.getCurrentInstance();
        setEditableTimeSlot((TimeSlot) fc.getExternalContext().getRequestMap().get("timeSlot"));
        return "editTimeSlot";
    }
    //</editor-fold>

    //<editor-fold desc="CRUD Methods">
    public String addTimeSlot() {
        String ret;
        this.setStartTime();
        this.setEndTime();
        if (Driver.getLoggedDriverStatic() == null) {
            if (TimeSlotDAO.addTimeSlot(this)) {
                ret = "timeSlotList";
            } else {
                ret = "timeSlotAddFailure";
            }
        } else {
            if (TimeSlotDAO.addTimeSlot(this, Driver.getLoggedDriverStatic().getId())) {
                ret = "timeSlotList";
            } else {
                ret = "timeSlotAddFailure";
            }
        }
        this.setMessage("");
        return ret;
    }

    public String addTimeSlotPlus() {
        String ret = addTimeSlot();
        if (ret.equals("timeSlotList")) {
            ret = "addTimeSlot";
        }
        this.setMessage("Time Slot successfully added");
        return ret;
    }

    public String deleteTimeSlot() {
        if (TimeSlotDAO.deleteTimeSlot(this)) {
            return "timeSlotList";
        } else {
            return "timeSlotDeleteFailure";
        }
    }

    public List<TimeSlot> getTimeSlots() {
        return TimeSlotDAO.getTimeSlots();
    }

    public List<TimeSlot> getTimeSlotsByDriver() {
        Driver driver;
        if (Driver.getEditableDriverStatic() == null) {
            driver = Driver.getLoggedDriverStatic();
        } else {
            driver = Driver.getEditableDriverStatic();
        }
        return TimeSlotDAO.getTimeSlotsByDriver(driver.getId());
    }

    public String saveTimeSlot() {
        this.setStartTime();
        this.setEndTime();
        if (TimeSlotDAO.saveTimeSlot(this)) {
            return "timeSlotList";
        } else {
            return "timeSlotSaveFailure";
        }
    }
    //</editor-fold>

    //<editor-fold desc="Driver-specific TimeSlot methods">
    public String editTimeSlotDriver() {
        FacesContext fc = FacesContext.getCurrentInstance();
        setEditableTimeSlot((TimeSlot) fc.getExternalContext().getRequestMap().get("timeSlot"));
        return "editTimeSlotDriver";
    }

    public String saveTimeSlotDriver() {
        this.setStartTime();
        this.setEndTime();
        if (TimeSlotDAO.saveTimeSlot(this)) {
            return "timeSlotListDriver";
        } else {
            return "timeSlotSaveFailure";
        }
    }
    //</editor-fold>

    //<editor-fold desc="Logged-specifi TimeSlot Methods">
    public String addTimeSlotLogged() {
        String ret = addTimeSlot();
        if (ret.equals("timeSlotList")) {
            ret = "timeSlotListDriverLogged";
        }
        return ret;
    }

    public String editTimeSlotLogged() {
        editTimeSlot();
        return "editTimeSlotLogged";
    }
    
    public String addTimeSlotPlusLogged() {
        String ret = addTimeSlot();
        if (ret.equals("timeSlotList")) {
            ret = "addTimeSlotLogged";
        }
        this.setMessage("Time Slot successfully added");
        return ret;
    }
    //</editor-fold>

    @Override
    public String toString() {
        return (weekday + ": From " + startTime.toString() + " To " + endTime.toString());
    }
}
